package cc.mrbird.febs.material.service.impl;

import cc.mrbird.febs.common.domain.QueryRequest;
import cc.mrbird.febs.common.domain.Tree;
import cc.mrbird.febs.common.utils.SortUtil;
import cc.mrbird.febs.material.dao.MaterialApplyMapper;
import cc.mrbird.febs.material.dao.MaterialBasicMapper;
import cc.mrbird.febs.material.domain.MaterialApply;
import cc.mrbird.febs.material.domain.MaterialBasic;
import cc.mrbird.febs.material.domain.MaterialLibrary;
import cc.mrbird.febs.material.service.IMaterialApplyService;
import cc.mrbird.febs.material.service.MaterialBasicService;
import cc.mrbird.febs.system.domain.Dept;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author mg
 */
@Service
public class MaterialBasicServiceImpl extends ServiceImpl<MaterialBasicMapper, MaterialBasic> implements MaterialBasicService {

    @Override
    public IPage<MaterialBasic> findMaterialBasic(MaterialBasic materialBasic, QueryRequest queryRequest) {
        try {
            LambdaQueryWrapper<MaterialBasic> queryWrapper = new LambdaQueryWrapper<>();

            if (StringUtils.isNotBlank(materialBasic.getName())) {
                queryWrapper.like(MaterialBasic::getName, materialBasic.getName());
            }

            Page<MaterialBasic> page = new Page<>();
            SortUtil.handlePageSort(queryRequest, page, true);
            return this.page(page,queryWrapper);
        } catch (Exception e) {
            log.error("获取仓库失败", e);
            return null;
        }
    }

    @Override
    @Transactional
    public void createDept(MaterialBasic materialBasic) {
    /*    Long parentId = dept.getParentId();
        if (parentId == null)
            dept.setParentId(0L);*/
        materialBasic.setCreateTime(new Date());
        this.save(materialBasic);
    }

    @Override
    @Transactional
    public void updateDept(MaterialBasic materialBasic) {
        //materialBasic.set(new Date());
        this.baseMapper.updateById(materialBasic);
    }

    @Override
    @Transactional
    public void deleteDepts(String[] deptIds) {
        this.delete(Arrays.asList(deptIds));
    }



    private void delete(List<String> deptIds) {
        removeByIds(deptIds);

   /*     LambdaQueryWrapper<MaterialBasic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(MaterialBasic::getParentId, deptIds);
        List<MaterialBasic> depts = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(depts)) {
            List<String> deptIdList = new ArrayList<>();
            depts.forEach(d -> deptIdList.add(String.valueOf(d.getDeptId())));
            this.delete(deptIdList);
        }*/
    }
}
